from django.db import models

from apps.common.account.models import Account
from utils.common import BaseModel


class Category(models.Model):
    name = models.CharField(verbose_name='措施类别', max_length=200)
    sort_num = models.IntegerField(default=0, verbose_name='排序编号')

    class Meta:
        verbose_name = '一级指标（措施类别）'
        db_table = 'category'


class Question(models.Model):
    category = models.ForeignKey(Category, verbose_name='所属措施类别', null=True, on_delete=models.SET_NULL)
    name = models.CharField(verbose_name='题目', max_length=300)
    required = models.BooleanField(verbose_name='是否为必填', default=True)
    sort_num = models.IntegerField(default=0, verbose_name='排序编号')
    full_score = models.PositiveIntegerField(verbose_name='满分（百分比）', null=True)

    class Meta:
        verbose_name = '题目'
        db_table = 'question'


class Answer(BaseModel):
    # {"1": 0, "2": 1} key为题目的id，value为题目的选项（0-完成 1-未完成 2-不涉及)
    answer = models.JSONField(verbose_name='污染题目的回答')
    company_name = models.CharField(verbose_name='企业名称', max_length=200)
    people_name = models.CharField(verbose_name='企业联系人', max_length=50)
    telephone = models.CharField(verbose_name='企业联系人电话', max_length=50)
    address = models.CharField(verbose_name='企业地址', max_length=200)
    images = models.JSONField(verbose_name='现场图片', null=True)
    opinion = models.TextField(verbose_name='审核意见', default='')
    handler = models.CharField(verbose_name='审核人', max_length=50, default='')
    batch_num = models.PositiveIntegerField(verbose_name='批次', default=2)  # 1-第一批， 2-第二批

    spot_check_score = models.FloatField(verbose_name='抽查分数',  null=True, blank=True)

    class Meta:
        verbose_name = '回答'
        db_table = 'answer'


# 需要做应急预案的公司
class YingJiYuAnComp(models.Model):
    name = models.CharField(verbose_name='公司名', max_length=200)

    class Meta:
        verbose_name = '需要做应急预案的公司'
        db_table = 'ying_ji_yu_an'


# 需要做在线监控的公司
class ZaiXianJianKongComp(models.Model):
    name = models.CharField(verbose_name='公司名', max_length=200)

    class Meta:
        verbose_name = '需要做应急预案的公司'
        db_table = 'zai_xian_jian_kong'


# 企业自评
class CompanySelfAssessment(BaseModel):
    company_name = models.CharField(verbose_name='企业名称', max_length=200, unique=True)
    people_name = models.CharField(verbose_name='企业联系人', max_length=50)
    telephone = models.CharField(verbose_name='企业联系人电话', max_length=50)
    address = models.CharField(verbose_name='企业地址', max_length=200)
    self_assessment_table = models.JSONField(verbose_name='自评表的文件', null=False)
    category_1 = models.JSONField(verbose_name='指标1的文件', null=False)
    category_2 = models.JSONField(verbose_name='指标2的文件', null=False)
    category_3 = models.JSONField(verbose_name='指标3的文件', null=False)
    category_4 = models.JSONField(verbose_name='指标4的文件', null=False)
    category_5 = models.JSONField(verbose_name='指标5的文件', null=False)
    category_6 = models.JSONField(verbose_name='指标6的文件', null=False)
    category_7 = models.JSONField(verbose_name='指标7的文件', null=False)
    category_8 = models.JSONField(verbose_name='指标8的文件', null=False)

    check_opinion = models.TextField(verbose_name='检查意见', default='', blank=True)
    check_people = models.ForeignKey(Account, verbose_name='审核人', null=True, on_delete=models.SET_NULL)
    check_status = models.PositiveIntegerField(verbose_name='审核状态(0-待检查 1-通过 2-需要修改 3-已修改)', default=0)

    check_time = models.DateTimeField(verbose_name='检查时间', null=True)
    change_time = models.DateTimeField(verbose_name='修改时间', null=True)

    class Meta:
        verbose_name = '企业自评和佐证材料提交'
        db_table = 'company_self_assessment'


class FileTemplate(BaseModel):
    category = models.CharField(verbose_name='分类', default='其它', max_length=50)
    name = models.CharField(verbose_name='文件名', max_length=200)

    class Meta:
        verbose_name = '文件模板'
        db_table = 'file_template'
